﻿using System;
using System.Xml;
using System.Xml.XPath;
using System.Text;
using System.Data;
using System.Collections.Generic;
using System.Drawing;
using System.Web.UI.WebControls;

using DotNetNuke.Services.Search;
using DotNetNuke.Common.Utilities;

namespace SBPweb.DNN.PhotoGallery
{
    /// <summary>
    /// Summary description for PhotoGalleryViewerController
    /// </summary>
    public class PhotoGalleryController : DotNetNuke.Entities.Modules.ISearchable, DotNetNuke.Entities.Modules.IUpgradeable
    {
        /*
        static public List<PhotoGalleryInfo> GetPhotoGalleries(int ModuleId)
        {
            List<PhotoGalleryInfo> PhotoGalleryViewerInfolist = new List<PhotoGalleryInfo>();
            using (IDataReader dr = DataProvider.Instance().GetPhotoGalleries(ModuleId))
            {
                while (dr.Read())
                {
                    PhotoGalleryInfo colPhotoGalleryViewerInfo = new PhotoGalleryInfo();
                    colPhotoGalleryViewerInfo.PhotoGalleryId = Convert.ToInt32(dr["PhotoGalleryId"]);
                    colPhotoGalleryViewerInfo.ModuleId = ModuleId;
                    colPhotoGalleryViewerInfo.Title = Convert.ToString(dr["Title"]);
                    colPhotoGalleryViewerInfo.Source = Convert.ToString(dr["Source"]);
                    colPhotoGalleryViewerInfo.BasePath = Convert.ToString(dr["BasePath"]);
                    colPhotoGalleryViewerInfo.CreatedDate = Convert.ToDateTime(dr["CreatedDate"]);
                    PhotoGalleryViewerInfolist.Add(colPhotoGalleryViewerInfo);
                }
            }
            return PhotoGalleryViewerInfolist;
        }*/
        static public PhotoGalleryInfo GetPhotoGallery(int? PhotoGalleryId, int? ModuleId)
        {
            bool bGotResult = false;
            PhotoGalleryInfo photoGalleryInfo = new PhotoGalleryInfo();
            using (IDataReader dr = DataProvider.Instance().GetPhotoGallery(PhotoGalleryId, ModuleId))
            {
                while (dr.Read())
                {
                    photoGalleryInfo.PhotoGalleryId = Convert.ToInt32(dr["PhotoGalleryId"]);
                    photoGalleryInfo.ModuleId = Convert.ToInt32(dr["ModuleId"]);
                    photoGalleryInfo.Title = Convert.ToString(dr["Title"]);
                    if (!(dr["Source"] is DBNull))
                    {
                        photoGalleryInfo.Source = Convert.ToString(dr["Source"]);
                    }
                    if (!(dr["BasePath"] is DBNull))
                    {
                        photoGalleryInfo.BasePath = Convert.ToString(dr["BasePath"]);
                    }
                    photoGalleryInfo.CreatedDate = Convert.ToDateTime(dr["CreatedDate"]);
                    if (!(dr["Width"] is DBNull))
                    {
                        photoGalleryInfo.Width = Convert.ToString(dr["Width"]);
                    }
                    if (!(dr["Height"] is DBNull))
                    {
                        photoGalleryInfo.Height = Convert.ToString(dr["Height"]);
                    }
                    if (!(dr["Background"] is DBNull))
                    {
                        photoGalleryInfo.Background = Convert.ToString(dr["Background"]);
                    }
                    if (!(dr["Foreground"] is DBNull))
                    {
                        photoGalleryInfo.Foreground = Convert.ToString(dr["Foreground"]);
                    }
                    if (!(dr["ButtonBackground"] is DBNull))
                    {
                        photoGalleryInfo.ButtonBackground = Convert.ToString(dr["ButtonBackground"]);
                    }
                    if (!(dr["ButtonForeground"] is DBNull))
                    {
                        photoGalleryInfo.ButtonForeground = Convert.ToString(dr["ButtonForeground"]);
                    }
                    if (!(dr["ButtonBorder"] is DBNull))
                    {
                        photoGalleryInfo.ButtonBorder = Convert.ToString(dr["ButtonBorder"]);
                    }
                    if (!(dr["ButtonPlayForeground"] is DBNull))
                    {
                        photoGalleryInfo.ButtonPlayForeground = Convert.ToString(dr["ButtonPlayForeground"]);
                    }
                    if (!(dr["CaptionBackground"] is DBNull))
                    {
                        photoGalleryInfo.CaptionBackground = Convert.ToString(dr["CaptionBackground"]);
                    }
                    if (!(dr["CaptionForeground"] is DBNull))
                    {
                        photoGalleryInfo.CaptionForeground = Convert.ToString(dr["CaptionForeground"]);
                    }
                    if (!(dr["ContentBackground"] is DBNull))
                    {
                        photoGalleryInfo.ContentBackground = Convert.ToString(dr["ContentBackground"]);
                    }
                    if (!(dr["SelectedThumbnailBorder"] is DBNull))
                    {
                        photoGalleryInfo.SelectedThumbnailBorder = Convert.ToString(dr["SelectedThumbnailBorder"]);
                    }
                    if (!(dr["TrackerForeground"] is DBNull))
                    {
                        photoGalleryInfo.TrackerForeground = Convert.ToString(dr["TrackerForeground"]);
                    }
                    if (!(dr["CaptionOpacity"] is DBNull))
                    {
                        photoGalleryInfo.CaptionOpacity = Convert.ToDecimal(dr["CaptionOpacity"]);
                    }
                    if (!(dr["CaptionFont"] is DBNull))
                    {
                        photoGalleryInfo.CaptionFont = Convert.ToString(dr["CaptionFont"]);
                    }
                    if (!(dr["TrackerFont"] is DBNull))
                    {
                        photoGalleryInfo.TrackerFont = Convert.ToString(dr["TrackerFont"]);
                    }
                    if (!(dr["BorderRadius"] is DBNull))
                    {
                        photoGalleryInfo.BorderRadius = Convert.ToInt32(dr["BorderRadius"]);
                    }
                    if (!(dr["Padding"] is DBNull))
                    {
                        photoGalleryInfo.Padding = Convert.ToInt32(dr["Padding"]);
                    }
                    if (!(dr["CaptionVisible"] is DBNull))
                    {
                        photoGalleryInfo.CaptionVisible = Convert.ToBoolean(dr["CaptionVisible"]);
                    }
                    if (!(dr["ThumbnailsVisible"] is DBNull))
                    {
                        photoGalleryInfo.ThumbnailsVisible = Convert.ToBoolean(dr["ThumbnailsVisible"]);
                    }
                    if (!(dr["TrackerVisible"] is DBNull))
                    {
                        photoGalleryInfo.TrackerVisible = Convert.ToBoolean(dr["TrackerVisible"]);
                    }
                    bGotResult = true;
                }
                if (bGotResult)
                {
                    return photoGalleryInfo;
                }
            }
            return null;
        }
        public static void DeletePhotoGallery(PhotoGalleryInfo objPhotoGallery)
        {
            DataProvider.Instance().DeletePhotoGallery(objPhotoGallery.PhotoGalleryId, objPhotoGallery.ModuleId);
        }
        public static int AddPhotoGallery(PhotoGalleryInfo objPhotoGallery)
        {
            return ((int)(DataProvider.Instance().AddPhotoGallery(
                objPhotoGallery.ModuleId,
                objPhotoGallery.Title,
                objPhotoGallery.Source,
                objPhotoGallery.BasePath,
                objPhotoGallery.Width,
                objPhotoGallery.Height,
                objPhotoGallery.Background,
                objPhotoGallery.Foreground,
                objPhotoGallery.ButtonBackground,
                objPhotoGallery.ButtonForeground,
                objPhotoGallery.ButtonBorder,
                objPhotoGallery.ButtonPlayForeground,
                objPhotoGallery.CaptionBackground,
                objPhotoGallery.CaptionForeground,
                objPhotoGallery.ContentBackground,
                objPhotoGallery.SelectedThumbnailBorder,
                objPhotoGallery.TrackerForeground,
                objPhotoGallery.CaptionOpacity,
                objPhotoGallery.CaptionFont,
                objPhotoGallery.TrackerFont,
                objPhotoGallery.BorderRadius,
                objPhotoGallery.Padding,
                objPhotoGallery.CaptionVisible,
                objPhotoGallery.ThumbnailsVisible,
                objPhotoGallery.TrackerVisible,
                objPhotoGallery.AutoPlay)));
        }
        public static void UpdatePhotoGallery(PhotoGalleryInfo objPhotoGallery)
        {
            DataProvider.Instance().UpdatePhotoGallery(
                objPhotoGallery.PhotoGalleryId,
                objPhotoGallery.ModuleId,
                objPhotoGallery.Title,
                objPhotoGallery.Source,
                objPhotoGallery.BasePath,
                objPhotoGallery.Width,
                objPhotoGallery.Height,
                objPhotoGallery.Background,
                objPhotoGallery.Foreground,
                objPhotoGallery.ButtonBackground,
                objPhotoGallery.ButtonForeground,
                objPhotoGallery.ButtonBorder,
                objPhotoGallery.ButtonPlayForeground,
                objPhotoGallery.CaptionBackground,
                objPhotoGallery.CaptionForeground,
                objPhotoGallery.ContentBackground,
                objPhotoGallery.SelectedThumbnailBorder,
                objPhotoGallery.TrackerForeground,
                objPhotoGallery.CaptionOpacity,
                objPhotoGallery.CaptionFont,
                objPhotoGallery.TrackerFont,
                objPhotoGallery.BorderRadius,
                objPhotoGallery.Padding,
                objPhotoGallery.CaptionVisible,
                objPhotoGallery.ThumbnailsVisible,
                objPhotoGallery.TrackerVisible,
                objPhotoGallery.AutoPlay);
        }
        #region Utility Functions
        public static int ConvertNullInteger(object Field)
        {
            if (Field == DBNull.Value)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(Field);
            }
        }
        #endregion
        #region IUpgradable
        public string UpgradeModule(string Version)
        {
            return "Custom upgrade code goes here for Version: " + Version;
        }
        #endregion

        #region IPortable Members
        /*
        string DotNetNuke.Entities.Modules.IPortable.ExportModule(int ModuleID)
        {
            StringBuilder strXML = new StringBuilder();
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.OmitXmlDeclaration = true;
            XmlWriter Writer = XmlWriter.Create(strXML, settings);
            // Outer Loop - To build the Surveys
            List<PhotoGalleryInfo> colSurveys = GetPhotoGalleries(ModuleID);
            if ((colSurveys.Count > 0))
            {
                Writer.WriteStartElement("photogalleries");
                foreach (PhotoGalleryInfo colPhotoGalleryViewerInfo in colSurveys)
                {
                    Writer.WriteStartElement("photogallery");
                    Writer.WriteElementString("question",
                    colPhotoGalleryViewerInfo.Question);
                    Writer.WriteElementString("vieworder",
                    colPhotoGalleryViewerInfo.ViewOrder.ToString());
                    Writer.WriteElementString("createdbyuser",
                    colPhotoGalleryViewerInfo.CreatedByUser.ToString());
                    Writer.WriteElementString("createddate",
                    colPhotoGalleryViewerInfo.CreatedDate.ToShortDateString());
                    Writer.WriteElementString("optiontype",
                    colPhotoGalleryViewerInfo.OptionType.ToString());
                    // Inner Loop - To build the Options for each Survey
                    List<PhotoGalleryOptionInfo> colSurveyOptions =
                    PhotoGalleryOptionController.GetSurveyOptions(colPhotoGalleryViewerInfo.PhotoGalleryId);
                    if ((colSurveyOptions.Count > 0))
                    {
                        Writer.WriteStartElement("surveyoptions");
                        foreach (PhotoGalleryOptionInfo colPhotoGalleryViewerOptionInfo
                        in colSurveyOptions)
                        {
                            Writer.WriteStartElement("surveyoption");
                            Writer.WriteElementString("optionname",
                            colPhotoGalleryViewerOptionInfo.OptionName);
                            Writer.WriteElementString("iscorrect",
                            colPhotoGalleryViewerOptionInfo.IsCorrect.ToString());
                            Writer.WriteElementString("vieworder",
                            colPhotoGalleryViewerOptionInfo.ViewOrder.ToString());
                            Writer.WriteEndElement();
                        }
                        // Retrieve the next SurveyOption
                        Writer.WriteEndElement();
                    }
                    Writer.WriteEndElement();
                }
                Writer.WriteEndElement();
                Writer.Close();
            }
            else
            {
                // There is nothing to export
                return String.Empty;
            }
            return strXML.ToString();
        }
        void DotNetNuke.Entities.Modules.IPortable.ImportModule(int ModuleID, string Content, string Version, int UserID)
        {
            //Import the Surveys
            //Outer Loop - To insert the Surveys
            int intCurrentSurvey;
            XmlNode xmlSurveys =
            DotNetNuke.Common.Globals.GetContent(Content, "surveys");
            foreach (XmlNode xmlSurvey in xmlSurveys)
            {
                PhotoGalleryInfo PhotoGalleryViewerInfo = new PhotoGalleryInfo();
                PhotoGalleryViewerInfo.ModuleId = ModuleID;
                PhotoGalleryViewerInfo.Question = xmlSurvey["question"].InnerText;
                PhotoGalleryViewerInfo.ViewOrder =
                Convert.ToInt32(xmlSurvey["vieworder"].InnerText);
                PhotoGalleryViewerInfo.CreatedByUser =
                Convert.ToInt32(xmlSurvey["createdbyuser"].InnerText);
                PhotoGalleryViewerInfo.CreatedDate =
                Convert.ToDateTime(xmlSurvey["createddate"].InnerText);
                PhotoGalleryViewerInfo.OptionType =
                xmlSurvey["optiontype"].InnerText;
                // Add the Survey to the database
                intCurrentSurvey = AddSurvey(PhotoGalleryViewerInfo);
                //Inner Loop - To insert the Survey Options
                XmlNode xmlSurveyOptions =
                xmlSurvey.SelectSingleNode("surveyoptions");
                foreach (XmlNode xmlSurveyOption in xmlSurveyOptions)
                {
                    PhotoGalleryOptionInfo PhotoGalleryViewerOptionInfo = new
                    PhotoGalleryOptionInfo();
                    PhotoGalleryViewerOptionInfo.PhotoGalleryId = intCurrentSurvey;
                    PhotoGalleryViewerOptionInfo.OptionName =
                    xmlSurveyOption["optionname"].InnerText;
                    PhotoGalleryViewerOptionInfo.IsCorrect =
                    Convert.ToBoolean(xmlSurveyOption["iscorrect"].InnerText);
                    PhotoGalleryViewerOptionInfo.ViewOrder =
                    Convert.ToInt32(xmlSurveyOption["vieworder"].InnerText);
                    // Add the Survey to the database
                    PhotoGalleryOptionController.AddSurveyOption(PhotoGalleryViewerOptionInfo);
                }
            }
        }
        */
        #endregion
        #region ISearchable Members
        public SearchItemInfoCollection GetSearchItems(DotNetNuke.Entities.Modules.ModuleInfo ModInfo)
        {
            // Get the Surveys for this Module instance
            PhotoGalleryInfo PhotoGalleryViewerInfo = GetPhotoGallery(null, ModInfo.ModuleID);
            SearchItemInfoCollection SearchItemCollection = new SearchItemInfoCollection();
            SearchItemInfo SearchItem;
            SearchItem = new SearchItemInfo(ModInfo.ModuleTitle + " - " + PhotoGalleryViewerInfo.Title,
            PhotoGalleryViewerInfo.Source,
            0,
            PhotoGalleryViewerInfo.CreatedDate, ModInfo.ModuleID,
            Convert.ToString(PhotoGalleryViewerInfo.PhotoGalleryId),
            PhotoGalleryViewerInfo.Title);
            SearchItemCollection.Add(SearchItem);
            return SearchItemCollection;
        }
        #endregion
    }
}